package com.neu.listener.ww;

import com.neu.service.ww.GoodsService;

import javax.servlet.http.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CarListener implements HttpSessionListener,HttpSessionAttributeListener {
    @Override
    public void sessionCreated(HttpSessionEvent se) {

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {

    }

    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {

    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {
        HttpSession session=event.getSession();
        List<Map<String,Object>> carList= (List<Map<String, Object>>) session.getAttribute("carList");
        if(carList==null){
            return ;
        }
        List<List<String>> lists=new ArrayList<>();
        List<String> list=new ArrayList<>();
        for (int i = 0; i < carList.size(); i++) {
            list.add(carList.get(i).get("num").toString());
//            System.out.println(carList.get(i).get("NUM").toString());
            list.add(carList.get(i).get("G_ID").toString());
            lists.add(list);


        }
        GoodsService goodsService=new GoodsService();
        int row=goodsService.doUpdateAllNumById(lists);
        if (row>0){
            session.setAttribute("carList",carList);
        }
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent event) {

    }
}
